package com.example.springbootfirst.configuration;

import com.example.springbootfirst.dto.ResponseDto;
import com.example.springbootfirst.exception.BaseException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * created by Morgan on 2021/9/30
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseDto<Throwable> handleException(Throwable throwable) {
        BaseException baseException = (BaseException) throwable;
        return new ResponseDto<>(baseException.getCode(),baseException.getMsg(),throwable);
    }
}
